#include <QBitmap>
#include <QDebug>
#include <QMouseEvent>
#include "microdisplay.h"

MicroDisplay::MicroDisplay(QLabel *parent) :
    QLabel(parent)
{
    setWindowFlags(Qt::ToolTip);
    setGeometry(800,990,88,18);
    m_displayColor = QColor("blue");
    m_bPressed = false;
    m_point = QPoint(800,990);
    m_font = QFont("Arial", 12, QFont::Bold);
}

void MicroDisplay::setDisplayLabelSize(int width, int height)
{
    this->setFixedSize(width, height);
}

QBitmap MicroDisplay::createDisplayMask(int count)
{
    QFontMetrics fm(m_font);
    int labelLength = fm.width("Hello World!")+3;

    setDisplayLabelSize(labelLength, count*18);
    QLabel label("Hello World!");
    label.resize(labelLength,count*18);
    label.setStyleSheet("background-color:white; color:black;");
    label.setFont(m_font);
    label.setAlignment(Qt::AlignLeft);
    QBitmap bmp = QBitmap::grabWidget(&label, label.rect());

    return bmp;
}

void MicroDisplay::display()
{
    QBitmap map = createDisplayMask(1);

    QString str = QString::fromLatin1("background-color:%1").arg(m_displayColor.name());

    this->setStyleSheet(str);
    this->clear();
    this->setMask(map);
    this->raise();
    this->show();
}

void MicroDisplay::enterEvent (QEvent * event)
{
    this->clearMask();
    this->raise();
    this->setText("Hello World!");
    this->setStyleSheet("background-color: rgba(206, 255, 243, 150);color:blue;font:10pt hold");
}

void MicroDisplay::leaveEvent (QEvent * event)
{
    display();
}

void MicroDisplay::mouseMoveEvent(QMouseEvent *event)
{

    if(m_bPressed)
    {
        this->move(QPoint(event->globalPos() - m_point));
    }
}

void MicroDisplay::mousePressEvent(QMouseEvent *event)
{
    m_bPressed = true;
    m_point = QPoint(event->pos());

    this->setStyleSheet("background-color: rgba(127, 127, 127, 127);color:blue;font:10pt hold");
}

void MicroDisplay::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPressed = false;
    this->setStyleSheet("background-color: rgba(206, 255, 243, 150);color:blue;font:10pt hold");
}

void MicroDisplay::slotSetDisplayFont(QFont font)
{
    m_font = font;
    display();

    qDebug()<<"----------MicroDisplay----------font:"<<m_font.rawName();
    qDebug()<<"----------MicroDisplay----------font:"<<m_font.pointSize();
    qDebug()<<"----------MicroDisplay----------font:"<<m_font.bold();
}

void MicroDisplay::slotSetDisplayColor(QColor color)
{
    m_displayColor = color;
    display();
    qDebug()<<"----------MicroDisplay----------color:"<<m_displayColor.name();
}

void MicroDisplay::slotSetDisplayPosition(QPoint point)
{
    this->move(point);
    qDebug()<<"----------MicroDisplay----------point-X:"<<point.x();
    qDebug()<<"----------MicroDisplay----------point-Y:"<<point.y();
}

void MicroDisplay::setDisplayParameter(QFont font, QColor color, QPoint point)
{
    m_font = font;
    m_displayColor = color;
    display();

    this->move(point);

}

QFont MicroDisplay::getDisplayFont()
{
    return m_font;
}

QColor MicroDisplay::getDisplayColor()
{
    return m_displayColor;
}

QPoint MicroDisplay::getDisplayPosition()
{
    qDebug()<<"----------getDisplayPosition----------point-X:"<<pos().x();
    qDebug()<<"----------getDisplayPosition----------point-Y:"<<pos().y();

   return this->pos();
}
